const Koa = require("koa");
const router = require("koa-router")();
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.set("Access-Control-Allow-Origin", "*"); //允许来自所有域名请求(不携带cookie请求可以用*，如果有携带cookie请求必须指定域名)
  // ctx.set("Access-Control-Allow-Origin", "http://localhost:8080"); // 只允许指定域名http://localhost:8080的请求

  ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE"); // 设置所允许的HTTP请求方法

  ctx.set(
    "Access-Control-Allow-Headers",
    "x-requested-with, accept, origin, content-type"
  ); // 字段是必需的。它也是一个逗号分隔的字符串，表明服务器支持的所有头信息字段.
  // 服务器收到请求以后，检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后，确认允许跨源请求，就可以做出回应。

  ctx.set("Content-Type", "application/json;charset=utf-8"); // Content-Type表示具体请求中的媒体类型信息

  ctx.set("Access-Control-Allow-Credentials", true); // 该字段可选。它的值是一个布尔值，表示是否允许发送Cookie。默认情况下，Cookie不包括在CORS请求之中。
  // 当设置成允许请求携带cookie时，需要保证"Access-Control-Allow-Origin"是服务器有的域名，而不能是"*";

  ctx.set("Access-Control-Max-Age", 300); // 该字段可选，用来指定本次预检请求的有效期，单位为秒。
  // 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时，服务器会提前发送一次请求进行验证
  // 下面的的设置只本次验证的有效时间，即在该时间段内服务端可以不用进行验证

  ctx.set("Access-Control-Expose-Headers", "myData"); // 需要获取其他字段时，使用Access-Control-Expose-Headers，
  // getResponseHeader('myData')可以返回我们所需的值
  /*
  CORS请求时，XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段：
      Cache-Control、
      Content-Language、
      Content-Type、
      Expires、
      Last-Modified、
      Pragma。
  */

  /* 解决OPTIONS请求 */
  if (ctx.method == "OPTIONS") {
    ctx.body = "";
    ctx.status = 204;
  } else {
    await next();
  }
});

router.get("/", async (ctx, next) => {
  ctx.body = "api";
});

router.get("/banners", async (ctx, next) => {
  ctx.body = [
    {
      id: 1,
      imgUrl:
        "https://gw.alicdn.com/imgextra/i3/186/O1CN01PBcnnP1DFEI8EsvSf_!!186-0-lubanu.jpg",
      link: "",
      caption: "",
    },
    {
      id: 2,
      imgUrl:
        "https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg",
      link: "",
      caption: "",
    },
    {
      id: 3,
      imgUrl:
        "https://aecpm.alicdn.com/simba/img/TB15tIjGVXXXXcoapXXSutbFXXX.jpg",
      link: "",
      caption: "",
    },
    {
      id: 4,
      imgUrl:
        "https://img.alicdn.com/imgextra/i1/135/O1CN0176EGKi1Crs5pF6ONz_!!135-0-luban.jpg",
      link: "",
      caption: "",
    },
    {
      id: 5,
      imgUrl:
        "https://img.alicdn.com/imgextra/i1/71/O1CN01vX1Ltn1COYkBoLVuG_!!71-0-luban.jpg",
      link: "",
      caption: "",
    },
    {
      id: 6,
      imgUrl:
        "https://picasso.alicdn.com/imgextra/i3/374007/O1CNA1gzJUy3100f6a609e960b0b7653000p_!!374007-0-picassobanner.jpg",
      link: "",
      caption: "",
    },
  ];
});

router.get("/tabs", async (ctx, next) => {
  ctx.body = [
    {
      id: 1,
      title: "热门",
      link: "hot",
    },
    {
      id: 2,
      title: "男装",
      link: "men",
    },
    {
      id: 3,
      title: "百货",
      link: "department",
    },
    {
      id: 4,
      title: "运动",
      link: "sports",
    },
    {
      id: 5,
      title: "手机",
      link: "mobile",
    },
    {
      id: 6,
      title: "家纺",
      link: "homeTextiles",
    },
    {
      id: 7,
      title: "食品",
      link: "food",
    },
    {
      id: 8,
      title: "汽车",
      link: "car",
    },
    {
      id: 9,
      title: "水果",
      link: "fruits",
    },
    {
      id: 10,
      title: "电脑",
      link: "mac",
    },
    {
      id: 11,
      title: "母婴",
      link: "baby",
    },
    {
      id: 12,
      title: "美妆",
      link: "",
    },
    {
      id: 13,
      title: "内衣",
      link: "",
    },
    {
      id: 14,
      title: "家居",
      link: "",
    },
    {
      id: 15,
      title: "家装",
      link: "",
    },
    {
      id: 16,
      title: "手机",
      link: "",
    },
  ];
});

router.get("/channels", async (ctx, next) => {
  ctx.body = [
    {
      id: 1,
      icon:
        "https://gw.alicdn.com/tfs/TB1OIxTcLc3T1VjSZLeXXbZsVXa-183-144.png?getAvatar=1",
      title: "天猫新品",
      link: "xxxx",
    },
    {
      id: 2,
      icon:
        "https://gw.alicdn.com/tfs/TB1LvIxVAvoK1RjSZFDXXXY3pXa-183-144.png?getAvatar=1",
      title: "今日爆款",
      link: "xxxx",
    },
    {
      id: 3,
      icon:
        "https://gw.alicdn.com/tfs/TB19uWKXkCy2eVjSZPfXXbdgpXa-183-144.png?getAvatar=1",
      title: "天猫国际",
      link: "xxxx",
    },
    {
      id: 4,
      icon:
        "https://gw.alicdn.com/tfs/TB1DaMyVpzqK1RjSZFoXXbfcXXa-185-144.png?getAvatar=1",
      title: "饿了么",
      link: "xxxx",
    },
    {
      id: 5,
      icon:
        "https://gw.alicdn.com/tfs/TB1FucwVwHqK1RjSZFgXXa7JXXa-183-144.png?getAvatar=1",
      title: "天猫超市",
      link: "xxxx",
    },
    {
      id: 6,
      icon:
        "https://gw.alicdn.com/tfs/TB1llI3f4n1gK0jSZKPXXXvUXXa-183-144.png?getAvatar=1",
      title: "充值中心",
      link: "xxxx",
    },
    {
      id: 7,
      icon:
        "https://gw.alicdn.com/tfs/TB1O_AyVwHqK1RjSZFPXXcwapXa-183-144.png?getAvatar=1",
      title: "机票酒店",
      link: "xxxx",
    },
    {
      id: 8,
      icon:
        "https://gw.alicdn.com/tfs/TB19yZJVBLoK1RjSZFuXXXn0XXa-183-144.png?getAvatar=1",
      title: "金币庄园",
      link: "xxxx",
    },
    {
      id: 9,
      icon:
        "https://gw.alicdn.com/tfs/TB16ZYDk4n1gK0jSZKPXXXvUXXa-183-144.png?getAvatar=1",
      title: "阿里拍卖",
      link: "xxxx",
    },
    {
      id: 10,
      icon:
        "https://gw.alicdn.com/tfs/TB19dcwVyrpK1RjSZFhXXXSdXXa-183-144.png?getAvatar=1",
      title: "淘宝吃货",
      link: "xxxx",
    },
    {
      id: 11,
      icon:
        "https://gw.alicdn.com/tfs/TB1nBktVxTpK1RjSZR0XXbEwXXa-183-144.png?getAvatar=1",
      title: "分类",
      link: "xxxx",
    },
    {
      id: 12,
      icon:
        "https://gw.alicdn.com/tfs/TB1fcOKXkCy2eVjSZSyXXXukVXa-183-144.png?getAvatar=1",
      title: "天猫美食",
      link: "xxxx",
    },
    {
      id: 13,
      icon:
        "https://gw.alicdn.com/tfs/TB1tikBVAPoK1RjSZKbXXX1IXXa-183-144.png?getAvatar=1",
      title: "阿里健康",
      link: "xxxx",
    },
    {
      id: 14,
      icon:
        "https://gw.alicdn.com/tfs/TB1h1MnVCrqK1RjSZK9XXXyypXa-183-144.png?getAvatar=1",
      title: "口碑生活",
      link: "xxxx",
    },
    {
      id: 15,
      icon:
        "https://gw.alicdn.com/tfs/TB1CoEwVrvpK1RjSZFqXXcXUVXa-185-144.png?getAvatar=1",
      title: "闲鱼",
      link: "xxxx",
    },
    {
      id: 16,
      icon:
        "https://img.alicdn.com/tfs/TB1FyEazAY2gK0jSZFgXXc5OFXa-183-144.png?getAvatar=1",
      title: "会员中心",
      link: "xxxx",
    },
  ];
});

router.get("/ads", async (ctx, next) => {
  const { categories_like } = ctx.request.query;

  const resultMap = {
    hot: [
      {
        imageUrl:
          "https://gw.alicdn.com/imgextra/i3/146/O1CN01B0vvbc1CwuRs3bKQh_!!146-0-lubanu.jpg",
        link: "",
      },
    ],
    men: [
      {
        imageUrl:
          "https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg",
        link: "",
      },
    ],
    department: [],
    sports: [],
    mobile: [],
    homeTextiles: [],
    food: [],
    car: [],
    fruits: [],
    mac: [],
    baby: [],
  };
  ctx.body = resultMap[categories_like];
});

router.get("/products", async (ctx, next) => {
  const { categories_like } = ctx.request.query;

  const resultMap = {
    hot: [
      {
        id: 1,
        title: "北欧风宜居家厚沙发布全盖网红遮盖沙发巾春夏沙发垫盖布沙发套罩",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/207605856/TB2fwIZXVXXXXbBXXXXXXXXXXXX_!!207605856.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 12.4,
        priceDesc: "757人已拼成",
        buyerAvatars: [
          "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epicB2Yl8IOkzPPmmaFnlF8QWctm2qvxXy9sgWV3Jd1tPnGytQDF3l5WiajmdJCyFGeicPHyZ7RY09CA/132",
          "http://thirdwx.qlogo.cn/mmopen/vi_32/V5iahDgFYlQR5BmI81XBxb7luQjG74Okq38q6HT4ErVd4FzHUhNJomLmz8LjaBJpgKRtVOib5zKFVdGyon2nYgMA/132",
        ],
      },
      {
        id: 2,
        title: "日式18cm 1.5L 珐琅搪瓷西红柿汤锅含保鲜盖燃气电磁炉通用包邮",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/43623533/TB2e2BUol8lpuFjy0FnXXcZyXXa_!!43623533.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 28,
        priceDesc: "953人已拼成",
        buyerAvatars: [
          "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erTFRia1y0kNag9JGqibg2FdGibFwuRiask9Yu5W0N7hwNaY0OCYto6soFBCA9JaeEuHibLGDeZ5y8iaouA/132",
        ],
      },
      {
        id: 3,
        title: "日本制Studio M'彩色陶瓷八角杯奶茶咖啡雪糕马克杯",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i2/858389760/TB2eadrlaagSKJjy0FaXXb0dpXa_!!858389760.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 69.8,
        priceDesc: "25人已拼成",
        buyerAvatars: [
          "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erTFRia1y0kNag9JGqibg2FdGibFwuRiask9Yu5W0N7hwNaY0OCYto6soFBCA9JaeEuHibLGDeZ5y8iaouA/132",
        ],
      },
      {
        id: 4,
        title: "居家桌面收纳盒梳妆台化妆品护肤品办公桌杂物遥控器钥匙储物筐",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/2086254883/O1CN01pqieY71lwSVYWhDIt_!!0-item_pic.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 19.9,
        priceDesc: "18人已拼成",
        buyerAvatars: [
          "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eotaXQTq1KzUqn3JtY6yarib6dhcfUtSvZ81UgKiaXsHYH4EGySYsx7V3ebosBggtqia6UP4eIecYE9g/132",
        ],
      },
      {
        id: 5,
        title: "全棉加厚磨毛四件套1.5m纯棉3d床笠床单婚庆被套四件套纯棉1.8m床",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/1638215896/TB1fu0hSVXXXXbxaXXXXXXXXXXX_!!0-item_pic.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 289,
        priceDesc: "172人已拼成",
        buyerAvatars: [],
      },
      {
        id: 6,
        title: "家用冷水壶玻璃耐热高温凉白开水杯茶壶套装大容量北欧水瓶凉茶壶",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i4/79189289/O1CN013GVokM2IUPeDAw4or_!!0-item_pic.jpg_500x500q90.jpg_.webp",
        tags: [],
        price: 14.6,
        priceDesc: "11938人已拼成",
        buyerAvatars: [],
      },
      {
        id: 7,
        title: "北欧门口钥匙挂钩壁挂创意玄关挂衣钩墙壁上鹿角装饰置物架衣帽架",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/14933842/O1CN01vegig01eFgGo5Vh6N_!!0-item_pic.jpg_500x500q90.jpg_.webp",
        tags: [],
        price: 38,
        priceDesc: "491人已拼成",
        buyerAvatars: [],
      },
      {
        id: 8,
        title: "北欧风宜居家厚沙发布全盖网红遮盖沙发巾春夏沙发垫盖布沙发套罩",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/207605856/TB2fwIZXVXXXXbBXXXXXXXXXXXX_!!207605856.jpg_500x500q90.jpg_.webp",
        tags: [],
        price: 12.4,
        priceDesc: "757人已拼成",
        buyerAvatars: [],
      },
      {
        id: 9,
        title: "北欧风宜居家厚沙发布全盖网红遮盖沙发巾春夏沙发垫盖布沙发套罩",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/207605856/TB2fwIZXVXXXXbBXXXXXXXXXXXX_!!207605856.jpg_500x500q90.jpg_.webp",
        tags: [],
        price: 12.4,
        priceDesc: "757人已拼成",
        buyerAvatars: [],
      },
      {
        id: 10,
        title: "北欧风宜居家厚沙发布全盖网红遮盖沙发巾春夏沙发垫盖布沙发套罩",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/207605856/TB2fwIZXVXXXXbBXXXXXXXXXXXX_!!207605856.jpg_500x500q90.jpg_.webp",
        tags: [],
        price: 12.4,
        priceDesc: "757人已拼成",
        buyerAvatars: [],
      },
    ],
    men: [
      {
        imageUrl:
          "https://aecpm.alicdn.com/simba/img/TB1CWf9KpXXXXbuXpXXSutbFXXX.jpg_q50.jpg",
        link: "",
      },
    ],
    department: [],
    sports: [],
    mobile: [],
    homeTextiles: [],
    food: [],
    car: [],
    fruits: [],
    mac: [],
    baby: [],
  };
  ctx.body = resultMap[categories_like];
});

router.get("/productVariants", async (ctx, next) => {
  ctx.body = [
    {
      id: 1,
      productId: 1,
      name: "款式1",
      price: 19.9,
      listPrice: 58.9,
      productVariantImages: [
        {
          id: 1,
          imgUrl:
            "//img.alicdn.com/imgextra/i2/2453056050/TB2vBphsB8lpuFjy0FnXXcZyXXa_!!2453056050.jpg_1152x1920Q50s50.jpg",
          caption: "",
          link: "",
          productVariantId: 1,
        },
        {
          id: 2,
          imgUrl:
            "//img.alicdn.com/imgextra/i2/2453056050/TB2Pu0jsr8kpuFjy0FcXXaUhpXa_!!2453056050.jpg_1152x1920Q50s50.jpg",
          caption: "",
          link: "",
          productVariantId: 1,
        },
        {
          id: 3,
          imgUrl:
            "//img.alicdn.com/imgextra/i3/2453056050/TB2myPCsgJkpuFjSszcXXXfsFXa_!!2453056050.jpg_1152x1920Q50s50.jpg",
          caption: "",
          link: "",
          productVariantId: 1,
        },
      ],
      product: {
        id: 1,
        title: "北欧风宜居家厚沙发布全盖网红遮盖沙发巾春夏沙发垫盖布沙发套罩",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/207605856/TB2fwIZXVXXXXbBXXXXXXXXXXXX_!!207605856.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 12.4,
        priceDesc: "757人已拼成",
        buyerAvatars: [
          "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epicB2Yl8IOkzPPmmaFnlF8QWctm2qvxXy9sgWV3Jd1tPnGytQDF3l5WiajmdJCyFGeicPHyZ7RY09CA/132",
          "http://thirdwx.qlogo.cn/mmopen/vi_32/V5iahDgFYlQR5BmI81XBxb7luQjG74Okq38q6HT4ErVd4FzHUhNJomLmz8LjaBJpgKRtVOib5zKFVdGyon2nYgMA/132",
        ],
      },
    },
    {
      id: 2,
      productId: 1,
      name: "款式2",
      price: 9.9,
      listPrice: 38.9,
      productVariantImages: [
        {
          id: 1,
          imgUrl:
            "//gw.alicdn.com/bao/uploaded/i1/43623533/TB2e2BUol8lpuFjy0FnXXcZyXXa_!!43623533.jpg_500x500q90.jpg_.webp",
          caption: "",
          link: "",
          productVariantId: 1,
        },
        {
          id: 2,
          imgUrl:
            "//img.alicdn.com/imgextra/i2/2453056050/TB2Pu0jsr8kpuFjy0FcXXaUhpXa_!!2453056050.jpg_1152x1920Q50s50.jpg",
          caption: "",
          link: "",
          productVariantId: 1,
        },
        {
          id: 3,
          imgUrl:
            "//img.alicdn.com/imgextra/i3/2453056050/TB2myPCsgJkpuFjSszcXXXfsFXa_!!2453056050.jpg_1152x1920Q50s50.jpg",
          caption: "",
          link: "",
          productVariantId: 1,
        },
      ],
      product: {
        id: 1,
        title: "北欧风宜居家厚沙发布全盖网红遮盖沙发巾春夏沙发垫盖布沙发套罩",
        imageUrl:
          "//gw.alicdn.com/bao/uploaded/i1/207605856/TB2fwIZXVXXXXbBXXXXXXXXXXXX_!!207605856.jpg_500x500q90.jpg_.webp",
        tags: ["全网底价", "赶快买"],
        price: 12.4,
        priceDesc: "757人已拼成",
        buyerAvatars: [
          "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epicB2Yl8IOkzPPmmaFnlF8QWctm2qvxXy9sgWV3Jd1tPnGytQDF3l5WiajmdJCyFGeicPHyZ7RY09CA/132",
          "http://thirdwx.qlogo.cn/mmopen/vi_32/V5iahDgFYlQR5BmI81XBxb7luQjG74Okq38q6HT4ErVd4FzHUhNJomLmz8LjaBJpgKRtVOib5zKFVdGyon2nYgMA/132",
        ],
      },
    },
  ];
});

app.use(router.routes(), router.allowedMethods());

app.listen(3000);
